package com.opensource.component.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.opensource.component.mapper.UserInfoMapper;
import com.opensource.component.model.po.UserAmountPo;
import com.opensource.component.model.po.UserAmountPo.UserAmountTableShardingStrategy;
import com.opensource.component.model.po.UserInfoPo;
import com.opensource.component.model.request.UserInfoListRequest;
import com.opensource.component.query.wrapper.LambdaExtendWrapper;
import java.util.ArrayList;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * -表现
 *
 * @author ZonLen since on 2021-12-18 16:02:32
 */

@RestController
public class UserInfoController {

  private final UserInfoMapper userInfoMapper;

  public UserInfoController(UserInfoMapper userInfoMapper) {
    this.userInfoMapper = userInfoMapper;
  }


  @GetMapping("/user/info/list")
  public List<UserInfoPo> loadUserInfoList(@RequestBody UserInfoListRequest req) {
    final LambdaExtendWrapper<UserInfoPo> wrapper = new LambdaExtendWrapper<>(UserInfoPo.class);
    wrapper
        .eq(UserInfoPo::getId, 1L)
        .leftJoin(UserAmountPo.class, jw -> jw
            .eq(UserAmountPo::getAmount, 100)
            .eq(UserAmountPo::getUserId, UserInfoPo::getId)
            .select(UserAmountPo::getAmount)
            .shardingStrategy(new UserAmountTableShardingStrategy()))
        .eq(UserInfoPo::getAge, 20);

    userInfoMapper.selectListExtend(wrapper);
//    userInfoMapper.joinSelectCount(wrapper);
//    userInfoMapper.joinSelectOne(wrapper);
//    userInfoMapper.joinSelectPage(new Page<>(1, 10), wrapper);
    final List<UserInfoPo> userInfoPos = userInfoMapper.selectList(wrapper);
//    final Long aLong = userInfoMapper.selectCount(wrapper);
    return new ArrayList<>();
  }

  @GetMapping("/readCommit")
  @Transactional(rollbackFor = Exception.class)
  public UserInfoPo readCommit() {
    UserInfoPo userInfoPo = userInfoMapper.selectById(1L);
    System.out.println(userInfoPo);
    userInfoPo.setUserName("li si");
    userInfoMapper.updateById(userInfoPo);
    userInfoPo = userInfoMapper.selectById(1L);
    System.out.println(userInfoPo);
    return userInfoPo;
  }

  @GetMapping("/repeatUpdate")
  @Transactional(rollbackFor = Exception.class)
  public UserInfoPo repeatUpdate() {
    UserInfoPo userInfoPo = userInfoMapper.selectById(1L);
    System.out.println(userInfoPo);
    userInfoPo.setUserName("li si");
    userInfoMapper.updateById(userInfoPo);
    userInfoPo.setUserName("wang wu");
    userInfoMapper.updateById(userInfoPo);
    return userInfoPo;
  }

}

